﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Sector7.Framework.DataDictionary.CORS;
using Sector7.Framework.DataDictionary.Utility;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;
using Sector7.Framework.DataDictionary.Windows;
using Sector7.Framework.Enums;
using System.ComponentModel;
using Sector7.Framework.DataDictionary.Agents;
using Sector7.Framework.Consts;

namespace Sector7.Framework.DataDictionary.Controls
{
    /// <summary>
    /// DataElementTabItem.xaml 的交互逻辑
    /// </summary>
    public partial class DataElementTabItem : UserControl
    {
        public static readonly DependencyProperty DataGroupProperty = DependencyProperty.Register("DataGroup", typeof(S7_DataGroup), typeof(DataElementTabItem));

        [Description("获取或设置数据分组")]
        [Category("其他")]
        public S7_DataGroup DataGroup
        {
            get { return this.GetValue(DataGroupProperty) as S7_DataGroup; }
            set { this.SetValue(DataGroupProperty, value); }
        }

        public DataElementTabItem()
        {
            InitializeComponent();
        }

        private List<S7_DataElement> m_dataElements = null;

        private void DoModify()
        {
            var selectedItem = dtgMain.SelectedItem as S7_DataElement;

            if (selectedItem != null)
            {
                AddDataElementWindow modifyWindow = new AddDataElementWindow();
                modifyWindow.DataGroup = DataGroup;
                modifyWindow.DataElement = selectedItem;
                modifyWindow.InitializeWindow(CrudType.Update);
                modifyWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

                modifyWindow.ShowDialog();
            }
        }

        private void DoDelete()
        {
            var selectedItem = dtgMain.SelectedItem as S7_DataElement;

            if (selectedItem != null)
            {
                MessageBoxResult result = Alert.ShowDialog(AlertType.Question, "是否确认删除选中项？");

                if (result == MessageBoxResult.Yes)
                {
                    int optResult = DataElementServiceAgent.Delete(selectedItem.ElementId);

                    if (optResult == C_OperatorResultType.SUCCESS)
                    {
                        m_dataElements.Remove(selectedItem);
                        dtgMain.Items.Refresh();
                    }
                }
            }
        }

        private void btnAddDataElement_Click(object sender, RoutedEventArgs e)
        {
            AddDataElementWindow addWindow = new AddDataElementWindow();
            addWindow.DataGroup = DataGroup;
            addWindow.InitializeWindow(CrudType.Create);
            addWindow.AddSuccess += new EventHandler(addWindow_AddSuccess);

            addWindow.ShowDialog();
        }

        void addWindow_AddSuccess(object sender, EventArgs e)
        {
            var addDataElementWindow = sender as AddDataElementWindow;
            var newElement = addDataElementWindow.DataElement;

            switch (addDataElementWindow.Crud)
            {
                case CrudType.Create:
                    m_dataElements.Add(newElement);

                    ClientUtil.LoadSelectedItem(dtgMain, newElement);

                    addDataElementWindow.ResetWindow();
                    break;
                case CrudType.Update:
                    ClientUtil.UpdateToItems(m_dataElements, C_PrimaryKeyName.DATA_ELEMENT, newElement);
                    break;
                default:
                    break;
            }
        }

        private void GetData()
        {
            if (DataGroup != null)
            {
                lblGroupName.Content = DataGroup.GroupName;

                m_dataElements = DataElementServiceAgent.GetAll(DataGroup.GroupId);

                ClientUtil.LoadItemsSource(dtgMain, m_dataElements);
            }
        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (this.IsVisible)
            {
                GetData();
            }
        }

        private void dtgMain_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            DoModify();
        }

        private void btnModify_Click(object sender, RoutedEventArgs e)
        {
            DoModify();
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            DoDelete();
        }
    }
}
